package com.ljb.monitorview.monitor2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.gson.Gson;
import com.ljb.monitorview.R;
import com.ljb.monitorview.monitor3.NotifyData3;
import com.ljb.utilslibrary.commUtils.JsonKit;
import com.ljb.utilslibrary.commUtils.ThreadUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ljb on 2017/6/29.
 */

public class MainActivity2 extends AppCompatActivity {
    private static final String TAG = "MainActivity2";
    private MonitorView2 mv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initUI();

        getConfig();
//        getRealData();
    }

    /**
     * 获取真实数据
     */
    private void getRealData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("http://app.shukuang.cn/ZHSW_APP_INTERFACE/phone/monitor/getMonitorDatasByManageId_new.ajax?factoryId=1151041334&manageId=18&dataType=1").build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: 获取真实数据失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    Log.e(TAG, "onResponse: 获取真实数据成功: " + result);
                    final NotifyData3 notifyData3 = JsonKit.toObj(result, NotifyData3.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //设置动态刷新的数据
//                            mv.notifyData(notifyData3);
                        }
                    });
                }
            }
        });
    }

    private void initUI() {
        mv = (MonitorView2) findViewById(R.id.mv);
    }

    //"http://115.28.25.233:8585/ZHSW_APP_INTERFACE/queryMonitorLocationByManageId.ajax?factoryId=1151041127&manageId=5"

    //获取配置信息数据及位置点
    private void getConfig() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url("http://app.shukuang.cn/ZHSW_APP_INTERFACE/queryMonitorLocationByManageId.ajax?factoryId=1151041127&manageId=5").build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: 获取配置信息失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    Log.e(TAG, "onResponse: 获取配置信息成功: " + result);
                    final ConfigBean2 configBean2 = new Gson().fromJson(result, ConfigBean2.class);
                    ThreadUtils.runUIThread(new Runnable() {
                        @Override
                        public void run() {
                            mv.setConfig(configBean2);

                        }
                    });

                }
            }
        });
    }


}
